package io.bdmc.common.nbapi.sdk.analyze;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public  class GwUtil {
	/**
	 * Sting杞琤yte閿熸枻鎷烽敓浠嬫柟閿熸枻鎷�
	 * @param str
	 * @return
	 */
	public static byte[] strToByteArray(String str) {
	    if (str == null) {
	        return null;
	    }
	    byte[] byteArray = str.getBytes();
	    return byteArray;
	}
	/**
	 * byte閿熸枻鎷烽敓鏂ゆ嫹杞琒tring閿熸枻鎷烽敓鏂ゆ嫹
	 * @param byteArray
	 * @return
	 */
 public static String byteArrayToStr(byte[] byteArray) {
	    if (byteArray == null) {
	        return null;
	    }
	    String str = new String(byteArray);
	    return str;
	}
 


/**
 * 閿熸枻鎷烽敓鏂ゆ嫹杞敓鏂ゆ嫹閿熸枻鎷稧BK閿熸枻鎷�(16閿熸枻鎷烽敓鏂ゆ嫹閿熻鍑ゆ嫹閿熸枻鎷�)閿熸枻鎷锋瘡閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷�2閿熸枻鎷烽敓琛楁枻鎷�
 * @param chineseStr
 * @return
 * @throws Exception
 */
public static String Chinese2GBK(String chineseStr)throws Exception {
StringBuffer GBKStr = new StringBuffer();
byte[] GBKDecode = chineseStr.getBytes("gbk");
for (byte b : GBKDecode) 
	GBKStr.append(Integer.toHexString(b&0xFF));
return GBKStr.toString().toUpperCase();
}


/**
 * 16閿熸枻鎷烽敓鏂ゆ嫹GBK閿熻鍑ゆ嫹閿熸枻鎷疯浆閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹
 * @param GBKStr
 * @return
 * @throws Exception
 */
public static String GBK2Chinese(String GBKStr)throws Exception{
byte[] b = HexString2Bytes(GBKStr);
String chineseStr = new String(b, "gbk");//閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓杞款亷鎷风焊閿熸枻鎷烽敓鏂ゆ嫹閿燂拷
return chineseStr;
}

/**
 * 閿熸枻鎷�16閿熸枻鎷烽敓鏂ゆ嫹閿熻鍑ゆ嫹閿熸枻鎷疯浆閿熸枻鎷烽敓鏂ゆ嫹閿熻鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹
 * @param hexStr
 * @return
 */
public static byte[] HexString2Bytes(String hexStr) {
 byte[] b = new byte[hexStr.length() / 2];
 for (int i = 0; i < b.length; i++) 
   b[i]=(byte) Integer.parseInt(hexStr.substring(2*i,2*i+2),16);
return b;
}


/**
 * 閿熸枻鎷烽敓琛楁枻鎷烽敓鏂ゆ嫹閿熸枻鎷疯浆閿熸枻鎷烽敓鏂ゆ嫹16閿熸枻鎷烽敓鏂ゆ嫹閿熻鍑ゆ嫹閿熸枻鎷�
 * @param byteArray
 * @return
 */
public static final String bytesToHexString(byte[] byteArray){
 StringBuffer hexStr = new StringBuffer(byteArray.length*2);
 for (int i = 0; i < byteArray.length; i++) {
	 String sTemp= Integer.toHexString(0xFF& byteArray[i]);
	 int j=0;
     while(j<2-sTemp.length())
    	 {sTemp="0"+sTemp;j++;}
     hexStr.append(sTemp.toUpperCase());
   }
  return hexStr.toString();
}

/**
 * byte 杞�16閿熸枻鎷烽敓鏂ゆ嫹
 * @param b
 * @return
 */
public static String byte2Hex( byte b) {     
    
    String hex = Integer.toHexString(b & 0xFF);    
    if (hex.length() == 1) {    
      hex = '0' + hex;    
      
    return hex.toUpperCase() ;    
  }
	return hex;    
 
}  
/**
 * 閿熸枻鎷烽敓鏂ゆ嫹鏍￠敓鏂ゆ嫹浣�
 * @param datas
 * @return
 */
public static byte getXor(byte[] datas){  
 
   byte temp=datas[1];  
         
   for (int i = 2; i <datas.length-2; i++) {  
       temp ^=datas[i];  
   }  
 
   return temp;  
}  

/** 
* @閿熸枻鎷烽敓鏂ゆ嫹: BCD閿熸枻鎷疯浆涓�10閿熸枻鎷烽敓鐙¤揪鎷�(閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷�) 
* @閿熸枻鎷烽敓鏂ゆ嫹: BCD閿熸枻鎷� 
* @閿熸枻鎷烽敓锟�: 10閿熸枻鎷烽敓鐙¤揪鎷� 
*/  
public static String bcd2Str(byte[] bytes) {  
StringBuffer temp = new StringBuffer(bytes.length * 2);  
for (int i = 0; i < bytes.length; i++) {  
   temp.append((byte) ((bytes[i] & 0xf0) >>> 4));  
   temp.append((byte) (bytes[i] & 0x0f));  
}  
return temp.toString().substring(0, 1).equalsIgnoreCase("0") ? temp  
       .toString().substring(1) : temp.toString();  
}  

/** 
* @閿熸枻鎷烽敓鏂ゆ嫹: 10閿熸枻鎷烽敓鐙¤揪鎷疯浆涓築CD閿熸枻鎷� 
* @閿熸枻鎷烽敓鏂ゆ嫹: 10閿熸枻鎷烽敓鐙¤揪鎷� 
* @閿熸枻鎷烽敓锟�: BCD閿熸枻鎷� 
*/  
public static byte[] str2Bcd(String asc) {  
int len = asc.length();  
int mod = len % 2;  
if (mod != 0) {  
   asc = "0" + asc;  
   len = asc.length();  
}  
byte abt[] = new byte[len];  
if (len >= 2) {  
   len = len / 2;  
}  
byte bbt[] = new byte[len];  
abt = asc.getBytes();  
int j, k;  
for (int p = 0; p < asc.length() / 2; p++) {  
   if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {  
       j = abt[2 * p] - '0';  
   } else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {  
       j = abt[2 * p] - 'a' + 0x0a;  
   } else {  
       j = abt[2 * p] - 'A' + 0x0a;  
   }  
   if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {  
       k = abt[2 * p + 1] - '0';  
   } else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {  
       k = abt[2 * p + 1] - 'a' + 0x0a;  
   } else {  
       k = abt[2 * p + 1] - 'A' + 0x0a;  
   }  
   int a = (j << 4) + k;  
   byte b = (byte) a;  
   bbt[p] = b;  
}  
return bbt;  
}  
/**
 * DWORD杞� long
 * @param bytes
 * @return
 */
public static long DWord2long(byte[] bytes) {  
 
long addr;  
if(bytes.length==1){  
   addr = bytes[0] & 0xFF;  
}else{  
   addr = bytes[0] & 0xFF;  
   addr = (addr << 8) | (bytes[1] & 0xff) ;  
   addr = (addr << 8) | (bytes[2] & 0xff) ;  
   addr = (addr << 8) | (bytes[3] & 0xff) ;  
}  
return addr;  
} 
    /**
	 * 閹搭亜褰嘼yte閺佹壆绮�   娑撳秵鏁奸崣妯哄斧閺佹壆绮�
	 * @param b 閸樼喐鏆熺紒锟�
	 * @param off 閸嬪繐妯婇崐纭风礄缁便垹绱╅敍锟�
	 * @param length 闂€鍨
	 * @return 閹搭亜褰囬崥搴ｆ畱閺佹壆绮�
	 */
	public static byte[] subByte(byte[] b,int off,int length){
		byte[] b1 = new byte[length];
		System.arraycopy(b, off, b1, 0, length);
		return b1;
	} 
/**
 * DWORD杞� int
 * @param bytes
 * @return
 */
public static int DWord2Int(byte[] bytes) {  
	 
int addr;  
if(bytes.length==1){  
   addr = bytes[0] & 0xFF;  
}else{  
   addr = bytes[0] & 0xFF;  
   addr = (addr << 8) | (bytes[1] & 0xff) ;  
   addr = (addr << 8) | (bytes[2] & 0xff) ;  
   addr = (addr << 8) | (bytes[3] & 0xff) ;  
}  
return addr;  
} 

/**
 * WORD 杞� int
 * @param bytes
 * @return
 */
static int byteBE1Int(byte[] bytes) {  
 
int addr;  
if(bytes.length==1){  
   addr = bytes[0] & 0xFF;  
}else{  
   addr = bytes[0] & 0xFF;  
   addr = (addr << 8) | (bytes[1] & 0xff) ;  
}  
return addr;  
}
/**
 * int 杞琖ord
 * @param iValue
 * @return
 */
public static byte[] Int2Bytes_LE(int iValue){
byte[] rst = new byte[2];
// 閿熸枻鎷峰啓int閿熸枻鎷烽敓鏂ゆ嫹閿熸彮浼欐嫹閿熸枻鎷风焊閿燂拷
rst[0] = (byte)((iValue & 0xFF00) >> 8 );
// int 閿熸枻鎷烽敓鏂ゆ嫹閿熻妭璁规嫹閿熸枻鎷烽敓琛楁枻鎷�
rst[1] = (byte)(iValue & 0xFF);
return rst;
}
/**
 * int 杞珼word
 * @param iValue
 * @return
 */
public static byte[] Int2Bytes1_LE(int iValue){
    byte[] rst = new byte[4];
 
    rst[0] = (byte)((iValue & 0xFF000000) >> 24 );
    rst[1] = (byte)((iValue & 0xFF0000) >> 16 );
    rst[2] = (byte)((iValue & 0xFF00) >> 8 );
    rst[3] = (byte)(iValue & 0xFF);
    return rst;
}

/**
 * long 杞珼word
 * @param iValue
 * @return
 */
public static byte[] Long2Bytes_LE(long iValue){
    byte[] rst = new byte[4];
 
    rst[0] = (byte)((iValue & 0xFF000000) >> 24 );
    rst[1] = (byte)((iValue & 0xFF0000) >> 16 );
    rst[2] = (byte)((iValue & 0xFF00) >> 8 );
    rst[3] = (byte)(iValue & 0xFF);
    return rst;
}
/**
 * string 閿熸枻鎷�0
 * @param str
 * @param strLength
 * @return
 */
public static String strAddZero_left(String str, int strLength) {
    int strLen = str.length();
    if (strLen < strLength) {
        while (strLen < strLength) {
            StringBuffer sb = new StringBuffer();
            sb.append("0").append(str);
           
            str = sb.toString();
            strLen = str.length();
        }
    }

    return str;
}
/**
 *  string 閿熸彮璇ф嫹0
 * @param str
 * @param strLength
 * @return
 */
public static String strAddZero_right(String str, int strLength) {
    int strLen = str.length();
    if (strLen < strLength) {
        while (strLen < strLength) {
            StringBuffer sb = new StringBuffer();
            
            sb.append(str).append("0");//閿熸彮璇ф嫹0
            str = sb.toString();
            strLen = str.length();
        }
    }

    return str;
}
/**
 * 鍗侀敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熻鍑ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹鏍￠敓鏂ゆ嫹浣嶉敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹鍗侀敓鏂ゆ嫹閿熸枻鎷烽敓鐙＄鎷锋牎閿熸枻鎷蜂綅
 * @param s
 * @return
 */
public static String GetCheckCode(String s)
{
//    int i = 0, r = 0, len=0, itmp=0;
//    String tmp = "";
//    len = s.length() / 2;
//    for (i = 0; i < len; i++)
//    {
//        tmp = s.substring(i * 2 , i * 2+2);
//        itmp = GwUtil.HexToInt(tmp);
//        r = itmp ^ r;
//    }
//    String ret = Integer.toHexString(r).toUpperCase();
//    ret = strAddZero_left(ret,2);
//    return ret;
	byte[] buffers = HexString2Bytes(s);
    byte x = buffers[0];
    for (int i = 1; i < buffers.length ; i++)
    {
        x = (byte)(x ^ buffers[i]);
    }
    return byte2Hex(x);
	
	
	
}
/**
 * 鍗侀敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹杞琲nt
 * @param hex
 * @return
 */
public static int HexToInt(String hex) {
     return Integer.valueOf(hex,16);    
}

public static byte[] BuildTransferredRequest(byte[] buffers)
{
    /*
      閿熸枻鎷疯瘑浣嶉敓鏂ゆ嫹0x7e 閿熸枻鎷锋伅閿熷彨绛规嫹閿熸枻鎷�0x7e杞敓鏂ゆ嫹->0x7d 0x02閿熸枻鎷�
閿熸枻鎷锋伅閿熷彨绛规嫹閿熸枻鎷�0x7d杞敓鏂ゆ嫹涓洪敓鏂ゆ嫹0x7d 0x01閿熸枻鎷�
閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷锋伅杞敓钘夛細閿熸枻鎷锋伅閿熸枻鎷疯->閿熸枻鎷烽敓濮愬苟閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹->杞敓鏂ゆ嫹
閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷锋伅杞敓钘夛細杞敓钘夎繕鍘�->閿熸枻鎷疯瘉閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷�->閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷锋伅

     */
    //閿熸枻鎷烽敓鏂ゆ嫹杞敓鏂ゆ嫹
    List<Byte> bytesLt = new ArrayList<Byte>();
    for (Byte i = 0; i < buffers.length; i++)
    {
        if (buffers[i] == 0x7E)
        {
        	bytesLt.add((byte)0x7D);
        	bytesLt.add((byte)0x02);
        }
        else if (buffers[i] == 0x7D)
        {
        	bytesLt.add((byte)0x7D);
        	bytesLt.add((byte)0x01);
        }
        else
        {
        	bytesLt.add(buffers[i]);
        }
    }
    return listTobyte11(bytesLt);
}
/****
 * 閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷疯垳閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹鑾奸敓鐨嗭拷
 * @param data
 * @return
 */
public static byte[] rebackData(byte[] data) {
    List<Byte> dataList = new ArrayList<>();
    for (int i = 0; i < data.length; i++) {
        if (i < data.length - 1) {
            if (data[i] == (byte) 0x7d && data[i + 1] == (byte) 0x02) {
                dataList.add((byte) 0x7e);
            } else if (data[i] == (byte) 0x7d && data[i + 1] == (byte) 0x01) {
                dataList.add((byte) 0x7d);
            } else {
                dataList.add(data[i]);
            }
        } else {
            dataList.add(data[i]);
        }
    }

    byte[] result = new byte[dataList.size()];
    for (int i = 0; i < dataList.size(); i++) {
        result[i] = dataList.get(i);
    }
    return result;
}


public static String IntToHex(int n, int len)
{
    StringBuilder sb = new StringBuilder();
    sb.append(Integer.toHexString(n).toUpperCase());
    while (sb.length() < len)
    {
        sb.insert(0, "0");
    }
    return sb.toString();
}



/**
 * list<Byte> 杞� byte[] 鏁堥敓缁炲尅鎷烽敓鏉拌鎷烽敓绔綇鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鎻嚖鎷烽敓鏂ゆ嫹閿熻姤鎹�
 * @param list
 * @return
 */
private static byte[] listTobyte11(List<Byte> list) {
if (list == null || list.size() < 0)
return null;
byte[] bytes = new byte[list.size()];
int i = 0;
Iterator<Byte> iterator = list.iterator();
while (iterator.hasNext()) {
bytes[i] = iterator.next();
i++;
}
return bytes;
}
/**
 * 閿熷彨璁规嫹鏍￠敓鏂ゆ嫹浣嶉敓瑙掑嚖鎷烽敓鏂ゆ嫹纭�
 * @param buffers
 * @return
 */
public static boolean ValidateCheckCode(byte[] buffers)
{
//    if (buffers.length < 2) return false;
//    byte x = buffers[1];
//    for (int i = 2; i < buffers.length - 2; i++)
//    {
//        x = (byte)(x ^ buffers[i]);
//    }
//    return x ==buffers[buffers.length - 2];

    
    
    if (buffers.length < 1) return false;
    byte x = buffers[0];
    for (int i = 1; i < buffers.length - 1; i++)
    {
        x = (byte)(x ^ buffers[i]);
    }
    return x ==buffers[buffers.length - 1];
}
/**
 * 閿熸枻鎷峰彇byte[]
 * @param src
 * @param begin
 * @param count
 * @return
 */
public static byte[] subBytes(byte[] src, int begin, int count) {
    byte[] bs = new byte[count];
    for (int i=begin; i<begin+count; i++) bs[i-begin] = src[i];
    return bs;
}

public static byte[] add(byte[] data1, byte[] data2) {

    byte[] result = new byte[data1.length + data2.length];
    System.arraycopy(data1, 0, result, 0, data1.length);
    System.arraycopy(data2, 0, result, data1.length, data2.length);
    return result;
}

}
